package chapt03.triangle;

import java.awt.event.*;
import javax.media.opengl.GLProfile;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TriangleAWT implements ActionListener
{
	private static GLCanvas glcanvas;
	
 	static
	{
		// setting this true causes window events not to get sent on Linux if
		// you run from inside Eclipse
		GLProfile.initSingleton(false);
	}
	
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
 	private static void createAndShowGUI()
 	{
		GLProfile glprofile = GLProfile.getDefault();
		GLCapabilities glcapabilities = new GLCapabilities(glprofile);
		glcanvas = new GLCanvas(glcapabilities);

		glcanvas.addGLEventListener(new TriangleGLEventListener());
		
		glcanvas.addKeyListener(new TriangleKeyListener(glcanvas));       
        
        final Frame frame = new Frame("Triangle Culling Example");
        frame.add(glcanvas);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowevent)
            {
                frame.remove(glcanvas);
                frame.dispose();
                System.exit(0);
            }
        });

        //Create/set menu bar and content pane.
        TriangleAWT demo = new TriangleAWT();
 
        //Create and set up the popup menu.
        demo.createPopupMenu(glcanvas);
        
        frame.setSize(600, 400);
        frame.setVisible(true);			
 	}
 	
 	public void createPopupMenu(GLCanvas glcanvas)
 	{
        JMenuItem menuItem;
        
        //Create the popup menu.
        JPopupMenu popup = new JPopupMenu();
        menuItem = new JMenuItem("Toggle depth test");
        menuItem.addActionListener(this);
        popup.add(menuItem);
        menuItem = new JMenuItem("Toggle cull backface");
        menuItem.addActionListener(this);
        popup.add(menuItem);
        menuItem = new JMenuItem("Toggle outline back");
        menuItem.addActionListener(this);
        popup.add(menuItem);
 
        //Add listener to the text area so the popup menu can come up.
        MouseListener popupListener = new TrianglePopupListener(popup);
        glcanvas.addMouseListener(popupListener);
 	}
 	
	public static void main(String[] args)
	{
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
	}
	
    public void actionPerformed(ActionEvent e)
    {
        JMenuItem source = (JMenuItem)(e.getSource());
        String text = source.getText();
        if(text == "Toggle depth test")
        	Triangle.processMenu(1);
        else if(text == "Toggle cull backface")
        	Triangle.processMenu(2);
        else if(text == "Toggle outline back")
        	Triangle.processMenu(3);
        
        // Refresh the Window
		glcanvas.display();
    }
}
